#!/usr/bin/env python3
"""
创建信访记录测试数据
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from sqlalchemy.orm import Session
from core.database import SessionLocal
from models.petition_record import PetitionRecord
from models.user import User
from datetime import datetime, timedelta
import random

def create_petition_test_data():
    """创建信访记录测试数据"""
    db = SessionLocal()
    try:
        # 获取admin用户
        admin_user = db.query(User).filter(User.username == "admin").first()
        if not admin_user:
            print("Admin user not found")
            return

        # 测试数据
        test_regions = ["天河区", "越秀区", "海珠区", "荔湾区", "白云区", "黄埔区", "番禺区", "花都区"]
        test_complaint_types = ["环境污染", "噪音污染", "市容管理", "交通问题", "住房问题", "教育问题", "医疗问题"]

        # 创建20条测试信访记录
        for i in range(20):
            # 随机生成日期（最近30天内）
            days_ago = random.randint(0, 30)
            petition_date = datetime.now() - timedelta(days=days_ago)

            petition_record = PetitionRecord(
                xh=f"TEST{i+1:03d}",
                ccjbz=random.choice(["来信", "来访", "来电", "网上投诉"]),
                tsr=f"测试投诉人{i+1}",
                ts_gk=f"关于{random.choice(test_complaint_types)}的投诉",
                ts_xzq=random.choice(test_regions),
                ts_ds="广州市",
                ts_qxs=random.choice(test_regions),
                ts_jd=f"测试街道{random.randint(1,10)}",
                ts_dz=f"测试地址{i+1}号",
                ts_qxdw=f"测试去向单位{random.randint(1,5)}",
                ts_dfnr=f"关于{random.choice(test_complaint_types)}问题的答复",
                djsj=petition_date,
                ts_bz=f"这是第{i+1}条测试信访记录",
                xfnr=f"关于{random.choice(test_complaint_types)}的投诉，问题比较严重，希望相关部门能够尽快处理。具体情况如下：{random.choice(['噪音扰民', '空气污染', '垃圾处理', '交通拥堵'])}。",
                creator_id=admin_user.id,
                creator="admin"
            )

            db.add(petition_record)

        db.commit()

        print(f"成功创建 20 条信访记录测试数据")

    except Exception as e:
        print(f"创建测试数据失败: {e}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    create_petition_test_data()